package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> listAll() {
        List<Permission> list = permissionMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        /*if(count==0){
            return new PageResult<>(Collections.emptyList(),qo.getCurrentPage(),qo.getPageSize(),count);
        }else {
            List<Permission> permissions = permissionMapper.selectForList(qo);
            return new PageResult<>(permissions,qo.getCurrentPage(),qo.getPageSize(),count);
        }*/
         PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }
    @Autowired
    private ApplicationContext ctx;  //spring的容器对象

    @Override
    public void reload() {
        //获取数据库里所有的权限表达式
        List<String> expressions = permissionMapper.selectAllExpression();
        //根据注解拿到所有控制器bean
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取map里所有的value
        Collection<Object> beans = map.values();
        for (Object bean : beans) {
            //获取每个bean的字节码对象
            Class<?> beanClass = bean.getClass();
            //通过反射获取每个bean的方法
            Method[] methods = beanClass.getDeclaredMethods();
            for (Method method : methods) {
                RequiredPermission annotation = method.getDeclaredAnnotation(RequiredPermission.class);
                if(annotation!=null){
                    String expression = annotation.expression();
                    String name = annotation.name();
                    if(!expressions.contains(expression)){
                        //如果数据库没有这个权限,就封装权限对象保存到数据库
                        Permission  permission = new Permission();
                        permission.setName(name);
                        permission.setExpression(expression);
                        permissionMapper.insert(permission);
                        //插入之后把数据加到集合中
                        expressions.add(expression);
                    }
                }
            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        List<String> expressions = permissionMapper.selectByEmpId(id);
        return expressions;
    }
}
